#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include "Save.h"
using namespace std;
void Save::saveHero(Hero& hero) {
    ofstream file;
    string filename = R"(.\save\Hero.txt)";
    file.open(filename);
    if (file.is_open() == false) {
        cout << "cannot open" << endl;
        exit(-1);
    }
    file << hero.get_hp() << endl;
    file << hero.get_atk() << endl;
    file << hero.get_name() << endl;
    file << hero.get_xp() << endl;
    file << hero.get_level() << endl;
    file << hero.get_mp() << endl;
    file << hero.get_maxhp() << endl;
    file << hero.get_money() << endl;
    file << hero.get_maxxp() << endl;
    file << hero.get_maxmp() << endl;
    for (int i = 0;i < 3;i++) {
        file << hero.get_skill(i) << endl;
    }
    file << hero.nowTask() << endl;
    file << hero.getWpnId() << endl;
    file << hero.getArmorId() << endl;
    for (int i = 0;i < 22;i++) {
        file << hero.getGoodsNum(i) << endl;
    }
    file.close();
}
void Save::saveRoom(roomPage& room) {
    ofstream file;
    string filename = R"(.\save\Room.txt)";
    file.open(filename);
    if (file.is_open() == false) {
        cout << "cannot open" << endl;
        exit(-1);
    }

    file << room.get_roomNumber() << endl;
    file << room.get_name() << endl;
    file << room.get_role().get_name() << endl;
    file << room.get_role().get_id() << endl;
    for (auto it : room.get_role().get_say()) {
        file << it << endl;
    }

    file.close();
}
void Save::loadHero(Hero& hero) {
    ifstream file;
    string filename = R"(.\save\Hero.txt)";
    file.open(filename);
    if (file.is_open() == false) {
        cout << "cannot open" << endl;
        exit(-1);
    }
    string buff;
    int buffer = 0;
    getline(file, buff);
    buffer = stod(buff);
    hero.set_hp(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_atk(buffer);

    getline(file, buff);
    hero.set_name(buff);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_xp(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_level(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_mp(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_maxhp(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_money(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_maxxp(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.set_maxmp(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    for (int i = 0;i < buffer;i++) {
        hero.learnSkill(0);
    }

    getline(file, buff);
    buffer = stoi(buff);
    for (int i = 0;i < buffer;i++) {
        hero.learnSkill(1);
    }

    getline(file, buff);
    buffer = stoi(buff);
    for (int i = 0;i < buffer;i++) {
        hero.learnSkill(2);
    }

    getline(file, buff);
    buffer = stoi(buff);
    for (int i = 0;i < buffer;i++) {
        hero.finishTask();
    }

    getline(file, buff);
    buffer = stoi(buff);
    hero.setWpn(buffer);

    getline(file, buff);
    buffer = stoi(buff);
    hero.setArmor(buffer);

    for (int i = 0;i < 22;i++) {
        getline(file, buff);
        buffer = stoi(buff);
        hero.add_Goods(i, buffer);
    }

    file.close();
}
void Save::loadRoom(roomPage& room) {

    ifstream file;
    ROOM_NUMBER r;
    string filename = R"(.\save\Room.txt)";
    file.open(filename);
    if (file.is_open() == false) {
        cout << "cannot open" << endl;
        exit(-1);
    }
    string buff;
    int buffer = 0;
    
    getline(file, buff);
    buffer = stoi(buff);
    r = ROOM_NUMBER(buffer);
    room.set_roomNumber(buffer);

    getline(file, buff);
    room.set_name(buff);

    getline(file, buff);
    room.get_role().set_name(buff);
    cout << buff << endl;

    getline(file, buff);
    buffer = stoi(buff);
    room.get_role().set_id(buffer);
    vector<string> new_say;

    while (getline(file, buff)) {
        new_say.push_back(buff);
    }
    room.get_role().set_say(new_say);
    room.changeRoom(r);

    file.close();
}